﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.IO;

namespace Xtreme_Server
{
    static class Program
    {
        /// <summary>
        /// Point d'entrée principal de l'application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            try
            {
                string sLine;
                StreamReader sr = new StreamReader(Path.Combine(Application.StartupPath, @"conf\server.dat"));

                while ((sLine = sr.ReadLine()) != null)
                {
                    //sLine.Trim();

                    if (sLine.Length > 0)
                    {
                        if (sLine.StartsWith("port "))
                        {
                            ws.Port = int.Parse(sLine.Split(' ')[1]);
                        }
                        else if (sLine.StartsWith("max "))
                        {
                            ws.MaxClient = int.Parse(sLine.Split(' ')[1]);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                
            }

            try
            {
                string sLine;
                StreamReader sr = new StreamReader(Path.Combine(Application.StartupPath, @"conf\ftp.dat"));

                while ((sLine = sr.ReadLine()) != null)
                {
                    //sLine.Trim();

                    if (sLine.Length > 0)
                    {
                        if (sLine.StartsWith("port "))
                        {
                            ftp.Port = int.Parse(sLine.Split(' ')[1]);
                        }
                        if (sLine.StartsWith("hellomsg "))
                        {
                            FTPWelcomeMsg = sLine.Substring(9);
                        }
                    }
                }
            }
            catch (Exception e)
            {

            }

            try
            {
                string sLine;
                StreamReader sr = new StreamReader(Path.Combine(Application.StartupPath, @"conf\ftpusers.dat"));

                while ((sLine = sr.ReadLine()) != null)
                {
                    //sLine.Trim();

                    if (sLine.Length > 0)
                    {
                        string user = sLine.Split(':')[0];
                        string pass = sLine.Split(':')[1];
                        ftp.Users.Add(new FTPUser(user, pass));
                    }
                }
            }
            catch (Exception e)
            {

            }

            //if (args == new string[0]/* || args[0] == null || args[0] == "-gui"*/)
            //{
                Application.Run(new MainWindowEx());
            //}
            //else
            //{
            //    if (args != null && args[0] == "-t")
            //    {
            //        if (args[1] == "-web")
            //        {
            //            int port = int.Parse(args[2].Replace("-port=", ""));
            //            ConsoleWebServer ws = new ConsoleWebServer(port);
            //        }
            //    }
            //}
        }

        public static WebServer ws = new WebServer();

        public static string WWWRoot = Path.Combine(Application.StartupPath, "wwwroot");
        public static string PHPDir = Path.Combine(Application.StartupPath, "php");
        public static string MySQLDir = Path.Combine(Application.StartupPath, "mysql");

        public static string FTPRoot = Path.Combine(Application.StartupPath, "ftproot");

        public static FTPServer ftp = new FTPServer();

        public static string PluginsDirectory = Path.Combine(Application.StartupPath, "lib");

        public static string FTPWelcomeMsg = "";
    }

    
}
